<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Picture;
use App\Models\GoodsEorderInfo;
use App\Repositories\DiscountRepository;
class Goods extends Model
{
    /**
     * 表名称
     * @var $table;
     */
    protected $table = 'goods';

    /**
     * 主键
     * @var $primaryKey
     */
    protected $primaryKey = 'goods_id';



    use SoftDeletes;

    protected $dates = ['deleted_at'];
    protected $appends = array('mobile_goods_thumb', 'discount', 'goods_real_price', 'goods_discount_tip', 'recode_excise_duty_percent', 'recode_value_added_tax_percent');
    /**
     * 自动填充字段
     * @var $fillable
     */

    protected $fillable = [
        'goods_id', 'cat_id', 'goods_sn', 'goods_bar_code', 'goods_name', 'goods_pet_name', 'goods_desc','brand_id','goods_number','goods_weight','market_price',
        'shop_price','keywords','goods_thumb','goods_gallery_id','is_on_sale',
        'add_time','sort_order','is_delete','is_home_recommend','is_cat_recommend','shop_id','sales_num','resource','type','deleted_at','search_keywords','audit_status'
    ];
    function getMobileGoodsThumbAttribute(){

        $goods_thumb = $this->getAttribute('goods_thumb');
        if(!$goods_thumb){
            return null;
        }

        if(Picture::find($goods_thumb)){
            return Picture::find($goods_thumb)->url;
        }else{
            return null;
        }

    }
    public function setGoodsSnAttribute($str)
    {
        $this->attributes['goods_sn'] = trim($str);
    }//end func

    public function setGoodsBarCodeAttribute($str)
    {
        $this->attributes['goods_bar_code'] = trim($str);
    }//end func

    public function setGoodsNameAttribute($str)
    {
        $this->attributes['goods_name'] = trim($str);
    }//end func

    public function setGoodsPetNameAttribute($str)
    {
        $this->attributes['goods_pet_name'] = trim($str);
    }//end func

    public function setGoodsDescAttribute($str)
    {
        $this->attributes['goods_desc'] = trim($str);
    }//end func

    public function setGoodsNumberAttribute($str)
    {
        $this->attributes['goods_number'] = trim($str);
    }//end func

    public function setKeywordsAttribute($str)
    {
        $this->attributes['keywords'] = trim($str);
    }//end func

    public function setResourceAttribute($str)
    {
        $this->attributes['resource'] = trim($str);
    }//end func




    function getGoodsRealPriceAttribute(){

        $goods_id = $this->getAttribute('goods_id');

        $shop_price = $this->getAttribute('shop_price');

        $discount = $this->getAttribute('discount');


        /*$discountRepository = new DiscountRepository();

        $discount = $discountRepository->getCanUseBiggestDiscountByGoodsId($goods_id);*/

        if ($discount) {

            // return sprintf("%.2f",sprintf("%.3f", $shop_price * intval($discount['discount_value']) / 100));

            $goods_price = sprintf("%.2f",sprintf("%.3f", $shop_price * $discount['discount_value'] / 100));

            if ($goods_price>0) {

                return $goods_price;
            }else{

                return 0.01;
            }
        }else{

            return $shop_price;
        }
    }


    function getDiscountAttribute(){

        $goods_id = $this->getAttribute('goods_id');

        $shop_price = $this->getAttribute('shop_price');


        $discountRepository = new DiscountRepository();

        $discount = $discountRepository->getCanUseBiggestDiscountByGoodsId($goods_id);

        if ($discount) {

            return $discount->toArray();
        }

        return null;
    }


    function getGoodsDiscountTipAttribute(){

        $goods_id = $this->getAttribute('goods_id');

        $goods_real_price = $this->getAttribute('goods_real_price');

        $discount = $this->getAttribute('discount');


        if ($discount) {

            //折扣提示
            $goods_discount_tip = '';

            if ($discount['discount_value'] % 10 == 0) {
                $goods_discount_tip = $discount['discount_value'] / 10;
            }else{

                $goods_discount_tip = intval($discount['discount_value'] / 10) . '.' . $discount['discount_value'] % 10;;
            }
            $goods_discount_tip .= '折';

            return $goods_discount_tip;
        }

        return null;
    }

    function getRecodeExciseDutyPercentAttribute(){


        $goods_sn = $this->getAttribute('goods_sn');

        $goods_eorder_info = GoodsEorderInfo::where('goods_sn', $goods_sn)->first();

        if ($goods_eorder_info) {

            return $goods_eorder_info->excise_duty_percent;
        }

        /*if ($goods_eorder_info && $goods_eorder_info->excise_duty_percent) {

            return $goods_eorder_info->excise_duty_percent;
        }else{

            return 0;
        }*/
    }

    function getRecodeValueAddedTaxPercentAttribute(){


        $goods_sn = $this->getAttribute('goods_sn');

        $goods_eorder_info = GoodsEorderInfo::where('goods_sn', $goods_sn)->first();

        if ($goods_eorder_info) {

            return $goods_eorder_info->value_added_tax_percent;
        }

        /*if ($goods_eorder_info && $goods_eorder_info->value_added_tax_percent) {

            return $goods_eorder_info->value_added_tax_percent;
        }else{

            return 0;
        }*/
    }
}
